﻿using Engine;
using Engine.Graphics;
using Game;
using GameEntitySystem;
using NekoMeko.Common;
using Neorxna;
using Neorxna.Common;
using Neorxna.NeoModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TemplatesDatabase;

namespace NekoMeko.Components
{
	public class ComponentNekoMekoOuterModel : Component, INeoModel
	{
		public ComponentModel TargetComponent
		{
			get; set;
		}
		public ComponentNekoMekoModel ComponentNekoMekoModel
		{
			get; set;
		}
		public ComponentHumanModel ComponentHumanModel
		{
			get; set;
		}
		public NekoResModel ResModel => ComponentNekoMekoModel.ResModel;
		public NekoResSkin ResSkin => ComponentNekoMekoModel.ResSkin;
		public Model Model => TargetComponent?.Model;
		public bool ShouldOverride(ComponentModel componentModel) => componentModel.GetType() == typeof(ComponentOuterClothingModel);

		public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
		{
			base.Load(valuesDictionary, idToEntityMap);
			TargetComponent = Entity.FindComponent<ComponentOuterClothingModel>(throwOnError: true);
			ComponentNekoMekoModel = Entity.FindComponent<ComponentNekoMekoModel>(throwOnError: true);
			ComponentHumanModel = Entity.FindComponent<ComponentHumanModel>(throwOnError: true);
		}
		public bool SetModel(Model model)
		{
			bool skip = false;
			if (ResModel != null)
			{
				if ((ResModel.ResPath?.TryGetValue("OuterClothingModelName", out var modelName) ?? false) && !string.IsNullOrEmpty(modelName))
				{
					var model1 = ResModel.GetRes(modelName) as Model;
					TargetComponent.m_model = model1;
					if (model1 != null)
					{
						TargetComponent.m_boneTransforms = new Matrix?[model1.Bones.Count];
						TargetComponent.AbsoluteBoneTransformsForCamera = new Matrix[model1.Bones.Count];
						TargetComponent.MeshDrawOrders = [.. Enumerable.Range(0, model1.Meshes.Count)];
						skip = true;
					}
				}
			}
			return skip;
		}
		public bool Animate()
		{
			bool flag0 = ResModel?.AllowClotheOverride ?? false;
			if (!flag0)//不允许衣物覆盖，跳过防止报错
				return true;
			TargetComponent.Opacity = ComponentHumanModel.Opacity;
			foreach (ModelBone bone in Model.Bones)
			{
				ModelBone modelBone = ComponentHumanModel.Model.FindBone(bone.Name);
				TargetComponent.SetBoneTransform(bone.Index, ComponentHumanModel.GetBoneTransform(modelBone.Index));
			}
			if (TargetComponent.Opacity.HasValue && TargetComponent.Opacity.Value < 1f)
			{
				bool num = ComponentHumanModel.m_componentCreature.ComponentBody.ImmersionFactor >= 1f;
				bool flag = NeorxnaResources.SubsystemSky.ViewUnderWaterDepth > 0f;
				TargetComponent.RenderingMode = num == flag ? ModelRenderingMode.TransparentAfterWater : ModelRenderingMode.TransparentBeforeWater;
			}
			else
			{
				TargetComponent.RenderingMode = ModelRenderingMode.AlphaThreshold;
			}

			return true;
		}
		public bool DrawExtras(Camera camera)
		{
			return false;
		}

		public bool Render(SubsystemModelsRenderer.ModelData modelData, ModelShader modelShader, Camera camera)
		{
			bool flag = ResModel?.AllowClotheOverride ?? false;
			return !flag;
		}

	}
}
